package cc.shacocloud.luanniao.core.service.vector.pgvector

import cc.shacocloud.luanniao.core.service.vector.VectorService
import kotlinx.coroutines.runBlocking
import org.springframework.beans.factory.InitializingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.r2dbc.core.await
import org.springframework.stereotype.Service

/**
 * 基于 pg 向量拓展实现的
 *
 * https://github.com/pgvector/pgvector
 *
 * 每次启动使用 `CREATE EXTENSION IF NOT EXISTS vector;` 创建拓展
 * @author 思追(shaco)
 */
@Service
@ConditionalOnProperty(prefix = "luanniao.vector", value = ["type"], havingValue = "pgvector", matchIfMissing = true)
class PgvectorService(val databaseClient: DatabaseClient) : VectorService, InitializingBean {

    override fun afterPropertiesSet() = runBlocking {
        databaseClient.sql("CREATE EXTENSION IF NOT EXISTS vector").await()
    }

    override fun destroy() {
    }

}